package com.nk.iexam.ds;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.nk.iexam.models.TheoryReview;

public class TheoryReviewDataSource extends AbstractObjectDataSource<TheoryReview>
{
	public static final String TABLE_NAME = "tblTheoryReview";
	public static final String CATEGORY_ID = "CategoryID";
	public static final String QUESTION = "Question";
	public static final String ANSWER = "Answer";
	
	public TheoryReviewDataSource()
	{
		super();
		_tableName = TABLE_NAME;
		_allColumnsName.add(CATEGORY_ID);
		_allColumnsName.add(QUESTION);
		_allColumnsName.add(ANSWER);
	}

	@Override
	protected TheoryReview cursorToObject(Cursor cursor_)
	{
		TheoryReview data = new TheoryReview();
		data.setId(cursor_.getLong(0));
		data.setCategoryId(cursor_.getLong(1));
		data.setQuestion(cursor_.getString(2));
		data.setAnswer(cursor_.getString(3));
		
		return data;
	}
	
	public TheoryReview create(final long theoryReviewId_, final String question_, final String answer_, final String knowdledge_) 
	{
		ContentValues values = new ContentValues();
		values.put(CATEGORY_ID, theoryReviewId_);
		values.put(QUESTION, question_);
		values.put(ANSWER, answer_);
		
		long insertId = _database.insert(TABLE_NAME, null, values);
		Cursor cursor = _database.query(TABLE_NAME, getAllTableColumnsName(), ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		TheoryReview newObject = cursorToObject(cursor);
		cursor.close();
		return newObject;
	}

	public List<TheoryReview> getAllQuestionInCategory(final long categoryId_)
	{
		List<TheoryReview> dbObjects = new ArrayList<TheoryReview>();
		Cursor cursor = _database.query(TABLE_NAME, getAllTableColumnsName(), "CategoryID = " + categoryId_, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			TheoryReview dbObject = cursorToObject(cursor);
			dbObjects.add(dbObject);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return dbObjects;
	}
}
